<!DOCTYPE html>
<html lang="en">
	<head>
		<title>预算单位</title>
		<#include "../inc/header.ftl"/>
	</head>

	<body class="no-skin">

		<div class="main-container ace-save-state" id="main-container">

			<div class="main-content">
				<div class="main-content-inner">

					<div class="page-content">
						<div class="row">
                            <div id="grid-main-data" class="col-xs-12">
                                <table id="MainGrid" style="min-height: 500px"></table>
								<div id="toolbar">
                                    <div style="padding:8px">
                                        区划：<input id="cantonid" name="cantonid">
                                        <a href="#" onclick="javascript:searchInfo()" class="easyui-linkbutton" iconCls="icon-search">查 询</a>
                                        <a href="#" onclick="javascript:findInfo()" class="easyui-linkbutton" iconCls="icon-redo">主动获取预算单位</a>
                                    </div>
								</div>
							</div>
						</div><!-- /.row -->
					</div><!-- /.page-content -->
                    <div id="box-add" class="ui-dialog-content hide">
                        <form class="form-horizontal" id="form-add" method="post">
                            <div class="form-group">
                                <label class="control-label col-xs-12 col-sm-4 no-padding-right">区划</label>
                                <div class="col-xs-12 col-sm-8">
									<span class="block input-icon input-icon-right">
										<input type="text" name="canton" id="canton" class="col-xs-12 easyui-textbox" data-options="required:true" style="width:100%" />
									</span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-xs-12 col-sm-4 no-padding-right">单位编码</label>
                                <div class="col-xs-12 col-sm-8">
                                    <div class="clearfix">
                                        <input type="text" name="unitcode" id="unitcode" class="col-xs-12 easyui-textbox" data-options="required:true" style="width:100%" />
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div><!-- #dialog-confirm -->

                </div>
			</div><!-- /.main-content -->
		</div><!-- /.main-container -->

		<!-- basic scripts -->
		<#include "../inc/basicjs.ftl"/>

		<!-- inline scripts related to this page -->
		<script type="text/javascript">
            var cantontree;
			jQuery(function($) {
                $('#MainGrid').datagrid({
                    url: prefix + '/base/basedata/findUnitPage',
                    checkbox: true,
                    pagination: true,
                    fitColumns:true,
                    striped: true,
                    singleSelect: true,
                    loadMsg: "正在加载数据，请稍后...",
                    queryParams: {},
                    columns:[[
                        { title: '单位ID',field: 'unitid',width:50},
                        { title: '单位编码',field: 'unitcode',  width: 100},
                        { title: '单位名称',field: 'unitname',width:300},
                        { title: '区划编码',field: 'cantoncode',width:100},
                        { title: '区划名称',field: 'cantonname',width: 300 }
                    ]],
                    toolbar: '#toolbar',
                    onBeforeLoad: function (param) {
                        // 首次打开时，不加载数据
                        var firstLoad = $(this).attr("firstLoad");  
                        if (firstLoad == "false" || typeof (firstLoad) == "undefined") {  
                            $(this).attr("firstLoad","true");  
                            return false;  
                        }  
                        return true;  
                    },
                    onLoadSuccess: function (data) {
                        if (data.total == 0) {
                            $('#grid-main-data').find('.datagrid-body').html('<div style="text-align: center;font-size: 14px;padding: 20px 0;">查询无数据</div>');
                        }
                    }
                });

                $(window).resize(function() {
                    $('#MainGrid').datagrid('resize');
                });
                $.ajax({
                    url:"${prefix}/base/basedata/findAllCantonMap",
                    type: 'POST',
                    async: false,
                    dataType: 'json',
                    success:function(data){
                        cantontree=data;
                    }
                });
                $('#cantonid').combotree({
                    data: cantontree,
                    required: true,
                    width: 200
                });
			});

            function searchInfo() {
                if ($('#cantonid').val() == '') {
                    $.messager.alert('提示', '请选择区划', 'info');
                    return;
                }
                $('#MainGrid').datagrid('load', {
                    cantonid: $('#cantonid').val(),
                });
            }

            function findInfo() {
                //---区划三级下拉框
                $('#canton').combotree({
                    data: cantontree,
                    required: true,
                    width: 200
                });

                $( "#box-add" ).removeClass('hide').dialog({
                    title: '&nbsp;主动获取预算单位',
                    iconCls:'icon-add',
                    width: 600,
                    height: 400,
                    modal: true,
                    buttons:[{
                        text:'获取',
                        iconCls:'icon-ok',
                        width:80,
                        plain:false,
                        handler:function(){
                            saveInfo();
                        }
                    },{
                        text:'取消',
                        iconCls:'icon-cancel',
                        width:80,
                        plain:false,
                        handler:function(){$('#box-add').dialog('close')}
                    }]
                });
            }
            function saveInfo() {
                var cantonid = $('#canton').val();
                var unitcode = $('#unitcode').val();
                $('#form-add').form({
                    url: prefix +'/base/basedata/getBaseUnit',
                    onSubmit:function(param){
                        param.cantonid = cantonid;
                        var isValid = $(this).form('validate');
                        if (isValid){
                            $.messager.progress({
                                title: '提示',
                                msg: '数据处理中，请稍候……',
                                text: ''
                            });
                        }
                        return isValid;
                    },
                    success:function(data) {
                        $.messager.progress('close');
                        var data = eval('(' + data + ')');
                        if(data.success) {
                            //$('#form-add').form('load',data.data);
                            $('#box-add').dialog('close');
                            searchInfo();
                        }
                        $.messager.alert('提示', data.msg, 'info');
                    }
                });
                $('#form-add').submit();
            }
        </script>
	</body>
</html>
